package com.itfreer.power.dao.jurisdiction;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.itfreer.form.api.JdbcBaseEntity;
import com.itfreer.power.dao.base.JdbcTreeEntityBase;
import com.itfreer.power.entity.jurisdiction.MapLayerJurisdictionItemEntity;
import com.itfreer.power.entity.jurisdiction.MapLayerJurisdictionEntity;

@Entity(name = "sso_jurisdiction_maplayer")
@Table(indexes = { @Index(name = "s_j_ml_code", columnList = "s_code"),
		@Index(name = "s_j_ml_pid", columnList = "pid") })
public class JdbcMapLayerJurisdictionEntity extends JdbcTreeEntityBase
		implements JdbcBaseEntity<MapLayerJurisdictionEntity> {

	private static final long serialVersionUID = 1102747120481795953L;

	/**
	 * 所属注册的服务
	 */
	@Column(name = "s_registerService", nullable = true)
	private String registerService;

	/**
	 * 子表对象
	 */
	@SuppressWarnings("deprecation")
	@org.hibernate.annotations.ForeignKey(name = "none")
	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
	@JoinColumn(name = "s_funid")
	private Set<JdbcMapLayerJurisdictionItemEntity> items = new HashSet<JdbcMapLayerJurisdictionItemEntity>();

	/**
	 * 所属注册的服务
	 */
	public String getRegisterService() {
		return registerService;
	}

	/**
	 * 所属注册的服务
	 */
	public void setRegisterService(String registerService) {
		this.registerService = registerService;
	}

	public Set<JdbcMapLayerJurisdictionItemEntity> getItems() {
		return items;
	}

	public void setItems(Set<JdbcMapLayerJurisdictionItemEntity> items) {
		this.items = items;
	}

	@Override
	public void from(MapLayerJurisdictionEntity entity) {
		this.setAbbreviation(entity.getAbbreviation());
		this.setCode(entity.getCode());
		this.setDisplay(entity.getDisplay());
		this.setId(entity.getId());
		this.setInfo(entity.getInfo());
		this.setLevel(entity.getLevel());
		this.setOrder(entity.getOrder());
		this.setPid(entity.getPid());
		this.setValid(entity.getValid());
		this.setLeaf(entity.getLeaf());
		this.setRegisterService(entity.getRegisterService());

		Set<JdbcMapLayerJurisdictionItemEntity> items = new HashSet<JdbcMapLayerJurisdictionItemEntity>();
		Set<MapLayerJurisdictionItemEntity> entityItems = entity.getItems();
		if (entityItems != null) {
			for (MapLayerJurisdictionItemEntity item : entityItems) {
				JdbcMapLayerJurisdictionItemEntity jEntity = new JdbcMapLayerJurisdictionItemEntity();
				jEntity.from(item);
				items.add(jEntity);
			}
		}
		this.setItems(items);
	}

	@Override
	public MapLayerJurisdictionEntity toEntity() {
		MapLayerJurisdictionEntity toEntity = new MapLayerJurisdictionEntity();
		toEntity.setAbbreviation(this.getAbbreviation());
		toEntity.setCode(this.getCode());
		toEntity.setDisplay(this.getDisplay());
		toEntity.setId(this.getId());
		toEntity.setInfo(this.getInfo());
		toEntity.setLevel(this.getLevel());
		toEntity.setOrder(this.getOrder());
		toEntity.setPid(this.getPid());
		toEntity.setValid(this.getValid());
		toEntity.setLeaf(this.getLeaf());
		toEntity.setRegisterService(this.getRegisterService());

		Set<MapLayerJurisdictionItemEntity> items = new HashSet<MapLayerJurisdictionItemEntity>();
		Set<JdbcMapLayerJurisdictionItemEntity> entityItems = this.getItems();
		if (entityItems != null) {
			for (JdbcMapLayerJurisdictionItemEntity item : entityItems) {
				items.add(item.toEntity());
			}
		}
		toEntity.setItems(items);

		return toEntity;
	}
}